<?php

namespace App\Controller;

use Common\Controller\WxAuthController;
use App\Logic;

class AppBaseController extends WxAuthController
{
    public function _initialize()
    {
        parent::_initialize();

        $this->handleOverTimeJoin();
    }

    /**
     * 处理超时任务
     *
     * @return void
     */
    private function handleOverTimeJoin()
    {
        $joinLogic = new Logic\JoinLogic();
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $overTimeJoinTime = date('Y-m-d H:i:s', time() - C('JOIN_OVERTIME'));

        $where = array(
            'user_id' => $userId,
            'state' => 1,
            'join_time' => array('ELT', $overTimeJoinTime),
        );

        $joinListRes = $joinLogic->getJoinList(1, 100, $where);
        $joinList = $joinListRes['Result']['list'];

        if (!empty($joinList)) {
            foreach ($joinList as $vo) {
                $joinLogic->cancelJoinTask(array(
                    'id' => $vo['id'],
                ));
            }
        }
    }
}
